<template>
    <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
    >
        <el-form-item label="产品名称" prop="name" class="length">
            <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>

        <el-form-item label="缴费类型" prop="payType" class="length">
            <el-input v-model="ruleForm.payType"></el-input>
        </el-form-item>

        <el-form-item label="适应人群" prop="suitableFor" class="length">
            <el-input v-model="ruleForm.suitableFor"></el-input>
        </el-form-item>

        <el-form-item label="可否续保" prop="renew" class="length">
            <el-radio v-model="ruleForm.renew" :label="true">是</el-radio>
            <el-radio v-model="ruleForm.renew" :label="false">否</el-radio>
        </el-form-item>

        <el-form-item label="保单形式" prop="policy" class="length">
            <!-- <el-input v-model="ruleForm.policy"></el-input> -->

            <el-checkbox-group v-model="ruleForm.policy">
                <el-checkbox label="纸质保单"></el-checkbox>
                <el-checkbox label="电子保单"></el-checkbox>
                <el-checkbox label="邮件保单"></el-checkbox>
                <el-checkbox label="美汁源保单"></el-checkbox>
            </el-checkbox-group>
        </el-form-item>

        <el-form-item label="价格" prop="price" class="length">
            <el-input v-model="ruleForm.price"></el-input>
        </el-form-item>

        <el-form-item label="所属分类" class="length">
            <!-- <el-input v-model="ruleForm.name"></el-input> -->
            <el-select v-model="ruleForm.values" placeholder="请选择保险类别">
                <el-option
                    v-for="item in options"
                    :key="item._id"
                    :label="item.name"
                    :value="item._id"
                >
                </el-option>
            </el-select>
        </el-form-item>

        <el-form-item label="上传图片" prop="name" class="length">
            <el-upload
                class="avatar-uploader"
                action="http://localhost:3000/insures/imagesInsures"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
            >
                <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icons"></i>
            </el-upload>
        </el-form-item>

        <el-form-item class="length">
            <el-button type="primary" @click="submitForm()">立即修改</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
    </el-form>
</template>


<script>
import axios from "axios";
export default {
    props: ["id"],

    data() {
        return {
            ruleForm: {
                name: "",
                payType: "",
                suitableFor: "",
                renew: "",
                policy: ["纸质保单"],
                price: "",
                values: "",
                imageUrl: "",
            },
            rules: {
                name: [
                    {
                        required: true,
                        message: "请输入产品名称",
                        trigger: "blur",
                    },
                ],
                payType: [
                    {
                        required: true,
                        message: "请输入缴费类型",
                        trigger: "blur",
                    },
                ],
                suitableFor: [
                    {
                        required: true,
                        message: "请输入适应人群",
                        trigger: "blur",
                    },
                ],
                renew: [
                    {
                        required: true,
                        // message: "是否可以续保",
                        trigger: "blur",
                    },
                ],
                policy: [
                    {
                        required: true,
                        message: "请输入保单形式",
                        trigger: "blur",
                    },
                ],
                price: [
                    {
                        required: true,
                        message: "请输入价格",
                        trigger: "blur",
                    },
                ],
            },
            imageUrl: "",
            options: [
                // {
                //     value: "黄金糕",
                //     label: "黄金糕",
                // },
                // {
                //     value: "双皮奶",
                //     label: "双皮奶",
                // },
                // {
                //     value: "蚵仔煎",
                //     label: "蚵仔煎",
                // },
                // {
                //     value: "龙须面",
                //     label: "龙须面",
                // },
                // {
                //     value: "北京烤鸭",
                //     label: "北京烤鸭",
                // },
            ],
            value: "",
        };
    },

    methods: {
        // 获取修改数据
        async handleClick() {
            // 发送请求
            const { data } = await axios({
                url: "/insures/getInsuresId",
                method: "get",
                params: {
                    id: this.id,
                },
            });
            console.log(data);
            if (data.code) {
                this.ruleForm.name = data.data[0].name;
                this.ruleForm.payType = data.data[0].payType;
                this.ruleForm.suitableFor = data.data[0].suitableFor;
                this.ruleForm.renew = data.data[0].renew;
                this.ruleForm.policy = data.data[0].policy;
                this.ruleForm.price = data.data[0].price;
                this.ruleForm.values = data.data[0].value.name;
                this.ruleForm.imageUrl = data.data[0].imageUrl;
                this.imageUrl ="http://localhost:3000/images/" + data.data[0].imageUrl;
            }
        },

        // 确认修改
        async submitForm() {
            // 发送请求
            const { data } = await axios({
                url: "/insures/updateInsures",
                method: "post",
                data: {
                    name: this.ruleForm.name,
                    payType: this.ruleForm.payType,
                    suitableFor: this.ruleForm.suitableFor,
                    renew: this.ruleForm.renew,
                    policy: this.ruleForm.policy,
                    price: this.ruleForm.price,
                    value: this.ruleForm.values,
                    imageUrl: this.ruleForm.imageUrl,
                    id: this.id,
                },
            });
            console.log(data);
            this.$router.push("/System/GetInsuresList");
        },

        resetForm(formName) {
            this.$refs[formName].resetFields();
        },

        // 图片
        handleAvatarSuccess(res, file) {
          console.log(res);
            if (res.code) {
                this.imageUrl = URL.createObjectURL(file.raw);
                this.ruleForm.imageUrl = res.data;
            }
        },

        beforeAvatarUpload(file) {
            const isJPG = file.type === "image/jpeg";
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
                this.$message.error("上传头像图片只能是 JPG 格式!");
            }
            if (!isLt2M) {
                this.$message.error("上传头像图片大小不能超过 2MB!");
            }
            return isJPG && isLt2M;
        },



            //获取保险分类
       async GetTypes(){
          const { data } = await axios({
              url:'/types/getTypes',
               method: "get",
          })
          console.log(data);
          this.options=data.data.types;
       }       

    },

    // 调用
    mounted() {
        this.handleClick();
         this. GetTypes();
    },
};
</script>
     
<style >
.length {
    width: 700px;
}
.avatar-uploader {
    height: 100px;
}
.avatar-uploader-icons {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
}

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader .el-upload:hover {
    border-color: #409eff;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}
.avatar {
    width: 100px;
    height: 100px;
    display: block;
}
</style>